/**
 * Created by gbolin on 2018/1/5.
 */

(function ($) {
    var drag = {
        maxSizeX: 10000000000000,
        minSizeX: 10,
        maxSizeY: 100000,
        minSizeY: 10,
        moveType: "all",
        endFn: null,
        isPercent: false
    };
    $.fn.drag = function (options) {
        var opts = $.extend({}, drag, options); //使用jQuery.extend 覆盖插件默认参数
        return this.each(function () {
            var $this = $(this);
            $this.css("position", "relative");
            var cssStyle = " position: absolute;width:8px;height:8px;cursor: nw-resize;bottom:0;right:0;";
            if (opts.moveType == "lr") {
                cssStyle = "position: absolute;width:4px;height:100%;cursor: e-resize;top:0;right:0;";
            } else if (opts.moveType == "up") {
                cssStyle = "position: absolute;width:100%;height:4px;cursor: n-resize;bottom:0;left:0;";
            }
            $this.append("<div class='dragTarget' style='" + cssStyle + "'></div>");
            $this.children(".dragTarget").on("mousedown", function (ev) {
                var that = this;
                var e = ev || event;
                var distanceX = e.clientX - $(this).position().left;
                var distanceY = e.clientY - $(this).position().top;
                moveMethod({
                    target: that,
                    moveX: distanceX,
                    moveY: distanceY
                });
                $(document).on("mouseup", function () {
                    $(document).unbind("mousemove");
                    $(document).unbind("mouseup");
                })

            })
        });
        function moveMethod(parameter) {
            $(document).on("mousemove", function (ev) {
                var e = ev || event;
                var tarParSizeW = 0;
                var endMoveW = 0;
                var tarParSizeH = 0;
                var endMoveH = 0;
                var distanceX, thatSizeW, maxSizeW, minSizeW, distanceY, thatSizeH, maxSizeH, minSizeH;
                distanceX = e.clientX - parameter.moveX;
                thatSizeW = $(parameter.target).outerWidth();
                maxSizeW = opts.maxSizeX;
                minSizeW = opts.minSizeX;
                distanceY = e.clientY - parameter.moveY;
                thatSizeH = $(parameter.target).outerHeight();
                maxSizeH = opts.maxSizeY;
                minSizeH = opts.minSizeY;
                var objx = {};
                var objy = {};
                if (opts.moveType == "lr") {
                    objx = judgeSizeX({
                        edW: endMoveW,
                        tpsW: tarParSizeW,
                        disX: distanceX,
                        tsw: thatSizeW,
                        maxsW: maxSizeW,
                        minsW: minSizeW
                    });
                    tarChange({
                        tar: $(parameter.target),
                        moveType: "left",
                        changeTar: "width",
                        move: objx.edw,
                        tarMove: objx.tpsw
                    });
                    opts.endFn&&opts.endFn($(parameter.target),objx)
                } else if (opts.moveType == "up") {
                    objy = judgeSizeY({
                        edH: endMoveH,
                        tpsH: tarParSizeH,
                        disY: distanceY,
                        tsH: thatSizeH,
                        maxsH: maxSizeH,
                        minsH: minSizeH
                    });
                    tarChange({
                        tar: $(parameter.target),
                        moveType: "top",
                        changeTar: "height",
                        move: objy.edh,
                        tarMove: objy.tpsh
                    });
                    opts.endFn&&opts.endFn($(parameter.target),objy);

                } else {
                    objx = judgeSizeX({
                        edW: endMoveW,
                        tpsW: tarParSizeW,
                        disX: distanceX,
                        tsw: thatSizeW,
                        maxsW: maxSizeW,
                        minsW: minSizeW
                    });
                    objy = judgeSizeY({
                        edH: endMoveH,
                        tpsH: tarParSizeH,
                        disY: distanceY,
                        tsH: thatSizeH,
                        maxsH: maxSizeH,
                        minsH: minSizeH
                    });
                    tarChange({
                        tar: $(parameter.target),
                        moveType: "left",
                        changeTar: "width",
                        move: objx.edw,
                        tarMove: objx.tpsw

                    });
                    tarChange({
                        tar: $(parameter.target),
                        moveType: "top",
                        changeTar: "height",
                        move: objy.edh,
                        tarMove: objy.tpsh
                    });
                    opts.endFn&&opts.endFn($(parameter.target),objx,objy);

                }
            });
        }
        function judgeSizeX(opt) {
            if (opt.disX + opt.tsw >= opt.maxsW) {
                opt.edW = opt.maxsW - opt.tsw;
                opt.tpsW = opt.maxsW;
            } else if (opt.disX + opt.tsw <= opt.minsW) {
                opt.edW = opt.minsW - opt.tsw;
                opt.tpsW = opt.minsW
            } else {
                opt.edW = opt.disX;
                opt.tpsW = opt.disX + opt.tsw;
            }

            return {edw: opt.edW, tpsw: opt.tpsW}

        }
        function judgeSizeY(opt) {
            if (opt.disY + opt.tsH >= opt.maxsH) {
                opt.edH = opt.maxsH - opt.tsH;
                opt.tpsH = opt.maxsH;
            } else if (opt.disY + opt.tsH <= opt.minsH) {
                opt.edH = opt.minsH - opt.tsH;
                opt.tpsH = opt.minsH;
            } else {
                opt.edH = opt.disY;
                opt.tpsH = opt.disY + opt.tsH;
            }
            return {edh: opt.edH, tpsh: opt.tpsH}
        }
        function tarChange(opts) {
            $(opts.tar).css(opts.moveType, opts.move + "px");
            $(opts.tar).parent().css(opts.changeTar, opts.tarMove + "px");
        }
    };
    var dragLayout={
        maxWidth:1000000,
        minWidth:5
    };
    $.fn.dragLayout = function (opctions) {
        var opts = $.extend({}, dragLayout, opctions);
        return $(this).drag({
            maxSizeX: opts.maxWidth,
            minSizeX: opts.minWidth,
            moveType: "lr",
            endFn:function (ele,obj) {
                var changer=ele.parent();
                var nextEle=changer.next();
                var parWidth=changer.parent().outerWidth();
                var t= obj.tpsw+"px";

                nextEle.css("width","calc(100% - "+t+")")
            }
        })

    };
})(jQuery);